package org.fage.semples.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.validation.Valid;

import org.fage.semples.model.CommonMessageResultVO;
import org.fage.semples.model.ExamSubjectVO;
import org.fage.semples.model.ExamTestTypeVO;
import org.fage.semples.model.ExamTestVO;
import org.fage.semples.model.PagerModel;
import org.fage.semples.service.MessageTestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

@Controller
public class ManageTestController {
	
	@Autowired
	private MessageTestService messageTestService;
	
	@ModelAttribute
	public ExamTestVO getExamTestVO(){
		return new ExamTestVO();
	}
	
	@RequestMapping(value = "/toManageTest")
	public String toManageTest(Model model){
		PagerModel<ExamTestVO> page = messageTestService.findAllByPage();
		model.addAttribute("url", "toManageTest");
		model.addAttribute("page_jsp", "1");
		model.addAttribute("page", page);
		return "teacher/manageTest";
	}
	
	@ResponseBody
	@RequestMapping(value = "/getTestById", method = RequestMethod.POST)
	public ExamTestVO getTestById(Integer testId){
		ExamTestVO examTestVO = messageTestService.getExamTestById(testId);
		return examTestVO;
	}
	
	@ResponseBody
	@RequestMapping(value = "/getSubjectAndTypes")
	public Map<String, Object> getSubjectAanTypes(){
		Map<String, Object> resultMap = new HashMap<String, Object>();
		List<ExamSubjectVO> subjectList = messageTestService.getSubjectList();
		List<ExamTestTypeVO> testTypeList = messageTestService.getTestTypeList();
		resultMap.put("subjectList", subjectList);
		resultMap.put("testTypeList", testTypeList);
		return resultMap;
	}
	
	//添加
	@ResponseBody
	@RequestMapping(value = "/saveTest", method = RequestMethod.POST)
	public CommonMessageResultVO saveTest(@Valid ExamTestVO examTestVO,BindingResult bindingResult){
		
		CommonMessageResultVO resultVO = new CommonMessageResultVO();
		Map<String, String> messageMap = new HashMap<String, String>();
		resultVO.setSuccess(false);
		if(bindingResult.hasErrors()){
			List<ObjectError> errorList = bindingResult.getAllErrors();
			for(ObjectError error : errorList){
				FieldError fe = (FieldError) error;
				messageMap.put(fe.getField(), fe.getDefaultMessage());
			}
			resultVO.setMessageMap(messageMap);
			return resultVO;
		}
		try{
			messageTestService.addTest(examTestVO);
			resultVO.setSuccess(true);
		}catch(Exception e){
			e.printStackTrace();
			messageMap.put("message", e.getMessage());
			resultVO.setMessageMap(messageMap);
		}
		return resultVO;
	}
	
	//修改
	@ResponseBody
	@RequestMapping(value = "/editTest", method = RequestMethod.POST)
	public CommonMessageResultVO editTest(@Valid ExamTestVO examTestVO,BindingResult bindingResult){
		CommonMessageResultVO resultVO = new CommonMessageResultVO();
		Map<String, String> messageMap = new HashMap<String, String>();
		resultVO.setSuccess(false);
		if(bindingResult.hasErrors()){
			List<ObjectError> errorList = bindingResult.getAllErrors();
			for(ObjectError error : errorList){
				FieldError fe = (FieldError) error;
				messageMap.put(fe.getField(), fe.getDefaultMessage());
			}
			resultVO.setMessageMap(messageMap);
			return resultVO;
		}
		try{
			messageTestService.editExamTest(examTestVO);
			resultVO.setSuccess(true);
		}catch(Exception e){
			e.printStackTrace();
			messageMap.put("message", e.getMessage());
			resultVO.setMessageMap(messageMap);
		}
		return resultVO;
	}
	
	//删除
	@RequestMapping(value = "/deleteTest/{id}", method = RequestMethod.GET)
	public String delete(RedirectAttributes redirectAttributes, @PathVariable Integer id){
		try {
			messageTestService.deleteExamTest(id);
			redirectAttributes.addFlashAttribute("message", "删除成功");
		} catch (Exception e) {
			redirectAttributes.addFlashAttribute("message", "删除失败");
			e.printStackTrace();
		}
		return "redirect:/toManageTest";
	}
}
